// TransformView.cpp : implementation of the CTransformView class
//

#include "stdafx.h"
#include "Transform.h"

#include "TransformDoc.h"
#include "TransformView.h"

#include <math.h>
#include "Global.h"
#include "SelectNodeDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CTransformView

IMPLEMENT_DYNCREATE(CTransformView, CView)

BEGIN_MESSAGE_MAP(CTransformView, CView)
	ON_WM_CREATE()
	ON_WM_DESTROY()
	ON_WM_SIZE()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_RBUTTONDBLCLK()
	ON_WM_TIMER()
	ON_WM_MOUSEMOVE()
	ON_WM_MBUTTONDOWN()
	ON_WM_MBUTTONUP()
	ON_WM_MOUSEWHEEL()
	ON_WM_RBUTTONDOWN()
	ON_WM_RBUTTONUP()
END_MESSAGE_MAP()

// CTransformView construction/destruction

CTransformView::CTransformView()
: m_pPlatform(NULL),
  m_pViewer(NULL),
  m_bPaning(false),
  m_bRotating(false),
  m_bSelecting(false)
{
	m_pPlatform = new COpenGL();
	m_pViewer = m_pPlatform->GetViewer();
}

CTransformView::~CTransformView()
{
	delete m_pPlatform;
	m_pPlatform = NULL;
}

BOOL CTransformView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

int CTransformView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	m_pPlatform->OnInitialize(this);

	return 0;
}

void CTransformView::OnDraw(CDC* pDC)
{
	// Start rendering.
	m_pPlatform->OnRender();

	// Swap buffers
	::SwapBuffers(pDC->GetSafeHdc());
}

void CTransformView::OnSize(UINT nType, int cx, int cy)
{
	CView::OnSize(nType, cx, cy);

	if(cx<=0 || cy<=0)
		return;

	m_pPlatform->OnSize(cx, cy);
}

void CTransformView::OnDestroy() 
{
	CView::OnDestroy();
}

// CTransformView message handlers
void CTransformView::OnLButtonDown(UINT nFlags, CPoint point)
{
	switch(m_pViewer->GetOperateType())
	{
	case CGViewer::ePan:
		m_bPaning = true;
		m_pViewer->StartPan(point.x, point.y);
		break;

	case CGViewer::eRotate:
		m_bRotating = true;
		m_pViewer->StartRotate(point.x, point.y);
		break;

	case CGViewer::ePreSelect:
		{
			CSelectSet* pSelectSet = CSKGlobal::GetSelectSet();
			if (pSelectSet->GetCount() == 1)
			{
				m_pViewer->SetOperateType(CGViewer::eSelect);

				ISelect* pObject = pSelectSet->GetObject(0);
				CTransNode* pNode = dynamic_cast<CTransNode*>(pObject);

				CDragNodeProcessCard* pDragNodeDef = CSKGlobal::GetTargetDoc()->GetDragNodeDef();
				pDragNodeDef->SetSelectObj(pNode);
				pDragNodeDef->SetStartDragPt(point.x, point.y);

				/*CLineArrowGraphics* pArrow = m_pViewer->GetPreviewObject();
				CBone* pBone = pNode->GetBone();
				pArrow->SetBasePt(CVec((*pBone)[0][0], (*pBone)[0][1], (*pBone)[0][2]));
				pArrow->SetArrowDirection(pBone->Direction());
				pArrow->SetArrowLength(pBone->Direction().Length());
				pArrow->InitializeByPixel(30);
				CSKGlobal::GetGraphicsManager()->Add(pArrow);*/
			}
			else if (pSelectSet->GetCount() >= 2)
			{
				CSelectNodeDlg dlg;
				dlg.DoModal();
				m_pViewer->SetOperateType(CGViewer::ePreSelectMore);
			}

			m_bSelecting = true;

			m_pViewer->Update(point.x, point.y);
		}
		break;

	case CGViewer::ePreSelectMore:
		{
			m_pViewer->SetOperateType(CGViewer::eSelect);
			CDragNodeProcessCard* pDragNodeDef = CSKGlobal::GetTargetDoc()->GetDragNodeDef();
			pDragNodeDef->SetStartDragPt(point.x, point.y);

			m_pViewer->Update(point.x, point.y);
		}
		break;
	}
}

void CTransformView::OnLButtonUp(UINT nFlags, CPoint point)
{
	switch (m_pViewer->GetOperateType())
	{
	case CGViewer::eRotate:
		m_bRotating = false;
		break;

	case CGViewer::ePan:
		m_bPaning = false;
		break;

	case CGViewer::eSelect:
		m_bSelecting = false;
		break;
	}

	m_pViewer->Update(point.x, point.y);
}

void CTransformView::OnRButtonDblClk(UINT nFlags, CPoint point)
{
}

void CTransformView::OnTimer(UINT_PTR nIDEvent)
{
}

void CTransformView::UpdateView()
{
	InvalidateRect(NULL, false);
	OnDraw(GetDC());
}

CDC* CTransformView::RefreshView()
{
	InvalidateRect(NULL, false);
	return GetDC();
}

void CTransformView::OnMouseMove(UINT nFlags, CPoint point)
{
	if (m_pViewer->GetOperateType() == CGViewer::ePreSelect)
	{
		CSelectSet* pSelectSet = CSKGlobal::GetSelectSet();
		pSelectSet->Clear();

		CSelectNodeProcessCard* pSelectNodeDef = CSKGlobal::GetTargetDoc()->GetSelectNodeDef();
		pSelectNodeDef->SetSelectPt(point.x, point.y);
		pSelectNodeDef->SetParent(NULL); // TODO
		CALLPIPELINE(CSKFactory::eSelectNode, OnExecute, CSelectNodeProcessCard, pSelectNodeDef, SELECTNODEDEF_INSTANCE);

		CHighLightSet* pHighLightSet = CSKGlobal::GetHighLightSet();
		pHighLightSet->Reset();
		for (long lIndex = 0; lIndex < pSelectSet->GetCount(); lIndex ++)
		{
			ISelect* pObject = pSelectSet->GetObject(lIndex);
			CTransNode* pNode = dynamic_cast<CTransNode*>(pObject);
			pHighLightSet->Add(pNode);
		}
	}

	if (m_pViewer->IsOperating())
	{
		switch (m_pViewer->GetOperateType())
		{
		case CGViewer::eRotate:
			if (m_bRotating) m_pViewer->OnRotate(point.x, point.y);
			break;

		case CGViewer::ePan:
			if (m_bPaning) m_pViewer->OnPan(point.x, point.y);
			break;

		case CGViewer::eSelect:
			if (m_bSelecting)
			{
				CDragNodeProcessCard* pDragNodeDef = CSKGlobal::GetTargetDoc()->GetDragNodeDef();
				if (pDragNodeDef->GetSelectObj() != NULL)
				{
					pDragNodeDef->SetStopDragPt(point.x, point.y);
					pDragNodeDef->SetEyePt(m_pViewer->GetCamera()->m_Eye);

					CALLPIPELINE(CSKFactory::eDragNode, OnExecute, CDragNodeProcessCard, pDragNodeDef, DRAGNODEDEF_INSTANCE);
				}

				pDragNodeDef->SetStartDragPt(point.x, point.y);
			}
			break;
		}
	}

	UpdateView();
	m_pViewer->Update(point.x, point.y);
}

void CTransformView::OnMButtonDown(UINT nFlags, CPoint point)
{
}

void CTransformView::OnMButtonUp(UINT nFlags, CPoint point)
{
}

BOOL CTransformView::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
	m_pViewer->OnZoom(1.0 + zDelta / abs(zDelta) / 10.0);

	UpdateView();

	m_pViewer->Update(pt.x, pt.y);

	return TRUE;
}

void CTransformView::OnRButtonDown(UINT nFlags, CPoint point)
{
}

void CTransformView::OnRButtonUp(UINT nFlags, CPoint point)
{
}